package com.xsjt.compare;

import java.math.BigDecimal;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtils;
import com.xsjt.bean.User;
import com.xsjt.util.CopyBeanProperty;
import net.sf.cglib.beans.BeanCopier;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;

/**  
 * ClassName:CompareTest
 * 测试 各种 复制 方法的性能
 * Date:     2017年7月19日 下午5:53:07
 * @author   Joe  
 * @version    
 * @since    JDK 1.8
 */
public class CompareTest {
	
	public static void main(String[] args) throws Exception {
		
		// 1.使用 apache 的 复制 方法
		User orig = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
		User dest = new User();
		long start = System.currentTimeMillis();
		BeanUtils.copyProperties(dest, orig);
		System.out.println("1.apache的方法 花费的时间：" + (System.currentTimeMillis() - start) + "ms");
		System.out.println("dest==" + dest);
		
		System.out.println("--------------------------------我是分割线------------------------------------------------");
		
		// 2.使用 spring 的 复制方法
		User source = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
		User target = new User();
		start = System.currentTimeMillis();
		org.springframework.beans.BeanUtils.copyProperties(source, target);
		System.out.println("2.spring的方法 花费的时间：" + (System.currentTimeMillis() - start) + "ms");
		System.out.println("target==" + target);
		
		System.out.println("--------------------------------我是分割线------------------------------------------------");
		
		// 3.使用 cglib 的复制方法
		User source2 = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
		User target2 = new User();
		start = System.currentTimeMillis();
		BeanCopier.create(source2.getClass(), target2.getClass(), false).copy(source2, target2, null);
		System.out.println("3.cglib的方法 花费的时间：" + (System.currentTimeMillis() - start) + "ms");
		System.out.println("target2==" + target2);
		
		System.out.println("--------------------------------我是分割线------------------------------------------------");
		
		// 4.使用 EZMorph 的复制方法
		User source3 = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
		User target3 = new User();
		start = System.currentTimeMillis();
		MorpherRegistry registry = new MorpherRegistry();
		registry.registerMorpher(new BeanMorpher(User.class, registry));
		target3 = (User) registry.morph(User.class, source3);
		System.out.println("4.EZMorph的方法 花费的时间：" + (System.currentTimeMillis() - start) + "ms");
		System.out.println("target3==" + target3);
		
		System.out.println("--------------------------------我是分割线------------------------------------------------");
		
		// 5.使用 自定义 的复制方法
		User oldUser = new User(1, "admin", "12345", "135555555555", new Date(), new BigDecimal(20000.88), "土豪");
		User newUser = new User();
		start = System.currentTimeMillis();
		CopyBeanProperty.copyproperty(oldUser, newUser);
		System.out.println("5.自定义的方法 花费的时间：" + (System.currentTimeMillis() - start) + "ms");
		System.out.println("newUser==" + newUser);
	}
}
